package com.ruoyi.web.controller.credit;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.credit.domain.EvaTask;
import com.ruoyi.credit.service.IEvaTaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 信用评价任务计划Controller
 *
 * @author zhulei
 * @date 2022-03-10
 */
@Controller
@RequestMapping("/credit/task")
public class EvaTaskController extends BaseController
{
    private String prefix = "credit/task";

    @Autowired
    private IEvaTaskService evaTaskService;

    @RequiresPermissions("credit:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 查询信用评价任务计划列表
     */
    @RequiresPermissions("credit:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EvaTask evaTask)
    {
        startPage();
        List<EvaTask> list = evaTaskService.selectEvaTaskList(evaTask);
        return getDataTable(list);
    }

    /**
     * 导出信用评价任务计划列表
     */
    @RequiresPermissions("credit:task:export")
    @Log(title = "信用评价任务计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EvaTask evaTask)
    {
        List<EvaTask> list = evaTaskService.selectEvaTaskList(evaTask);
        ExcelUtil<EvaTask> util = new ExcelUtil<EvaTask>(EvaTask.class);
        return util.exportExcel(list, "信用评价任务计划数据");
    }

    /**
     * 新增信用评价任务计划
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存信用评价任务计划
     */
    @RequiresPermissions("credit:task:add")
    @Log(title = "信用评价任务计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EvaTask evaTask)
    {
        return toAjax(evaTaskService.insertEvaTask(evaTask));
    }

    /**
     * 修改信用评价任务计划
     */
    @RequiresPermissions("credit:task:edit")
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        EvaTask evaTask = evaTaskService.selectEvaTaskByTaskId(taskId);
        mmap.put("evaTask", evaTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存信用评价任务计划
     */
    @RequiresPermissions("credit:task:edit")
    @Log(title = "信用评价任务计划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EvaTask evaTask)
    {
        return toAjax(evaTaskService.updateEvaTask(evaTask));
    }

    /**
     * 删除信用评价任务计划
     */
    @RequiresPermissions("credit:task:remove")
    @Log(title = "信用评价任务计划", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(evaTaskService.deleteEvaTaskByTaskIds(ids));
    }

    /**
     * 发布评价任务
     */
    @PostMapping( "/taskBegin")
    @ResponseBody
    public AjaxResult taskBegin(Long taskId)
    {
        return toAjax(evaTaskService.taskBegin(taskId));
    }

    /**
     * 结束评价任务
     */
    @PostMapping( "/taskEnd")
    @ResponseBody
    public AjaxResult taskEnd(Long taskId)
    {
        AjaxResult ajaxResult = new AjaxResult();
        int i = evaTaskService.taskEnd(taskId);

        if (i == -1){
            ajaxResult.put("msg", "无法结束，还有未完成的评分！");
        } else{
            ajaxResult.put("code", 0);
        }
        return ajaxResult;
    }
}
